package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {

    @Autowired
    private MongoTemplate template;

    @Autowired
    private StrategyCatalogRepository repository;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyService strategyService;
    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);
        repository.save(strategyCatalog);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        repository.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public StrategyCatalog get(String id) {
        StrategyCatalog strategyCatalog =repository.findById(id).orElse(null);
        return strategyCatalog;
    }

    @Override
    public List<StrategyCatalog> list() {
        return repository.findAll();
    }

    @Override
    public Page<StrategyCatalog> query(StrategyCatalogQuery qo) {
        //分页查询
        //查询对象
        //1: 查询总数:total
        Query query = new Query();
        long total = template.count(query, StrategyCatalog.class);
        if(total == 0){
            return Page.empty();  //空的分页对象
        }
        //2: 每页显示条数集合: list
        //设置页面显示条数, 还有当前页
        //1 当前页 2 每页的条数 3 排序规则 4 排序字段
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.DESC, "_id");
        query.with(pageable);
        List<StrategyCatalog> list = template.find(query, StrategyCatalog.class);
        for (StrategyCatalog catalog : list) {
            Destination destination = destinationService.get(catalog.getDestId());
            catalog.setDestName(destination.getName());
        }
        //3:封装page对象
        return new PageImpl<StrategyCatalog>(list,pageable,total);
    }

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        if (!(StringUtils.hasLength(strategyCatalog.getId()))){
            strategyCatalog.setId(null);
            this.save(strategyCatalog);
        }else {
            this.update(strategyCatalog);

        }
    }

    @Override
    public List<CatalogVO> getgroupList() {
        List<CatalogVO> list = new ArrayList<>();
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,
                Aggregation.group("destName").
                        push("name").as("names").
                        push("id").as("ids")
        );
        AggregationResults<Map> result = template.aggregate(agg, Map.class);
        List<Map> datas = result.getMappedResults();
        System.err.println(1111+""+datas.get(1));
        System.err.println(1111+""+datas.get(0));
        for (Map data : datas) {
            CatalogVO vo = new CatalogVO();
            vo.setDestName((String) data.get("_id"));

            List<Object> names = (List<Object>) data.get("names");
            List<Object> ids = (List<Object>) data.get("ids");
            List<Map<String, Object>> mm = new ArrayList<>();
            for (int i = 0; i < names.size(); i++) {
                Map<String, Object> vv = new HashMap<>();
                String name = names.get(i).toString();
                String id = ids.get(i).toString();
                vv.put("id", id);
                vv.put("name", name);
                mm.add(vv);
            }
            vo.setMapList(mm);
            list.add(vo);
        }
        System.out.println(list);
        return list;
    }

    /*@Override
    public Object findByIdDestinationId(String id) {
       List<StrategyCatalog> strategyCatalogs =  strategyCatalogService.queryCatalogByDestinationId(id);
        return strategyCatalogs;
    }*/

    @Override
    public List<StrategyCatalog> queryCatalogByDestinationId(String id) {
        List<StrategyCatalog> list = repository.findByDestId(id);
        for (StrategyCatalog catalog : list) {
          List<Strategy> list1 =  strategyService.findByCatalogId(catalog.getId());
          catalog.setStrategies(list1);
        }
        return list;
    }



}
